/**
 * sakuri创建于2017/4/18.
 */
;-function (window) {
    "use strict";
    var ready = {
        base_path: '/cmp/',//基础路径
        websocket_ip: null,//基础路径
        court_uid: null,//当前法庭uid,
        court_name: null,//当前法庭名
        level: null,//当前法庭等级
        court_list: null,//所有法庭列表
        user_uid: null,//登录用户自身唯一标识
        user_court_uid: null,//用户所在法庭uid
        user_court_name: null,//用户所在法庭名称
        user_court_level: null,//用户所在法庭等级
        user_name: null,//用户姓名
        flag_search: false //判断本次搜索是否是本地搜索
    };
    var option = {};
    var init = function () {
        // $.ajaxSetup({
        //     dataType:'json'
        // });
        init_ready();//加载预设变量
        core.init_navbar();//初始化顶部导航栏
        // console.log($('.navbar-nav').children('li').eq(4).html());
        $('.navbar-nav').children('li').eq(4).find('a').eq(1).attr('href', ready.base_path + 'case_count/court_on_time_count?courtUid=' + ready.court_uid);
    };

    function init_ready() {//初始化预设值
        ready.base_path = window.location.href.indexOf('cmp')?'/cmp/':'/';
        ready.user_uid = $.cookie('_user_uid');
        ready.websocket_ip = $.cookie('_ws_ip');
        ready.user_court_uid = $.cookie('_court_uid');
        ready.court_list = core.get_global();
        ready.user_court_name = $.cookie('_court_name');
        ready.user_court_level = $.cookie('_level');
        ready.court_uid = utils.get_url_param('courtUid') || ready.user_court_uid;
        var court_basic = base.get_court(ready.court_uid);
        ready.court_name = court_basic ? court_basic.name : '';
        ready.level = court_basic ? court_basic.level : '';
        ready.user_name = $.cookie('_username');
        var url_data_search = utils.get_url_param('_search');
        ready.flag_search = url_data_search ? url_data_search == 'true' : $.cookie('_search') == 1;
    }

    //内部核心属性
    var core = {
        get_global: function () {
            var global_list = localStorage._global_list;
            if (global_list) {
                return JSON.parse(localStorage._global_list);
            }
            $.ajaxSetup({async: false});
            $.get(ready.base_path + "basis/getCourtList", {courtUid: ready.user_court_uid}, function (result) {
                if (result && result.code == 0) {
                    global_list = result.data;
                    localStorage._global_list = JSON.stringify(global_list);
                } else {
                    console.error('警告！初始化数据失败！');
                }
            }, "json");
            $.ajaxSetup({async: true});
            return global_list;
        },
        init_navbar: function () {
            var url = window.location.href;
            var nav_all = ready.flag_search && url.indexOf("sys_court_status") < 0 && url.indexOf("sys_server_status") < 0 && url.indexOf("panel_living") < 0;
            var global_list = ready.court_list;
            var courtlist = [];
            $(global_list).each(function (index, court) {
                if (court.parentUid == ready.user_court_uid) {
                    var temp = {};
                    temp.uid = court.uid;
                    temp.name = nav_all ? court.navigationName : court.name;
                    temp.shortname = court.shortName;
                    temp.level = court.level;
                    temp.child = [];
                    courtlist.push(temp);
                }
            });
            $(courtlist).each(function (i, court) {
                $(global_list).each(function (index, child_court) {
                    if (child_court.parentUid == court.uid) {
                        var temp = {};
                        temp.uid = child_court.uid;
                        temp.name = nav_all ? child_court.navigationName : child_court.name;
                        temp.shortname = child_court.shortName;
                        temp.level = child_court.level;
                        courtlist[i].child.push(temp);
                    }
                });
            });
            var temp = {};
            var temp_court = base.get_court(ready.user_court_uid);
            temp.uid = temp_court.uid;
            temp.name = nav_all ? temp_court.navigationName : temp_court.name;
            temp.shortname = temp_court.shortName;
            temp.level = temp_court.level;
            courtlist.unshift(temp);
            var html = "";
            var nav_courts = $("#_nav_courts");
            console.log(ready.court_uid);
            $(courtlist).each(function (index, elem) { //第一次循环child
                if (elem.uid == ready.court_uid) {
                    nav_courts.find("span").html(elem.name);
                    nav_courts.find(".cmp-head-court-name").html(elem.name);
                }
                if (elem.child && elem.child.length > 0) {
                    html += '<li class="dropdown-submenu"><a tabindex="-1" href="javascript:base.change_court(\'' + elem.uid + '\')"><span class="context-span">' + elem.name + '</span></a>' +
                        '<ul class="dropdown-menu animated fadeInDown">';
                    $(elem.child).each(function (index2, elem2) {
                        if (elem2.uid == ready.court_uid) {
                            nav_courts.find("span").html(elem2.name);
                            nav_courts.find(".cmp-head-court-name").html(elem2.name);
                        }
                        html += '<li><a href="javascript:base.change_court(\'' + elem2.uid + '\')"><span class="context-span">' + elem2.name + '</span></a>';
                    });
                    html += '</ul></li>';
                } else {
                    html += '<li><a href="javascript:base.change_court(\'' + elem.uid + '\')"><span class="context-span">' + elem.name + '</span></a>';
                }
            });
            nav_courts.find("ul").append(html);
            $("#_nav_sys_mo").attr("href", ready.base_path + "system_monitor/sys_court_status?courtUid=" + ready.court_uid);
        }
    };
    //对外公开的方法
    var base = {
        get_court: function (_param_court_uid) {
            var court_basic = null;
            $(ready.court_list).each(function (index, elem) {
                if (_param_court_uid == elem.uid) {
                    court_basic = ready.court_list[index];
                }
            });
            return court_basic;
        },
        logout: function () {
            $.get(r.base_path + "oauth/signOut", {}, function () {
                utils.remove_cookie("_username");
                utils.remove_cookie("_court_name");
                utils.remove_cookie("_court_uid");
                utils.remove_cookie("_court_ip");
                utils.remove_cookie("_level");
                utils.remove_cookie("_user_uid");
                localStorage.clear();
                window.location.href = r.base_path + "login.html";
            });
        },
        back: function () {
            history.go(-1);
        },
        change_court: function (_param_court_uid) {
            $(ready.court_list).each(function (i, n) {
                if (n.uid == _param_court_uid) {
                    console.log(_param_court_uid, n.uid);
                    var uri = window.location.href;
                    if (uri.indexOf('courtUid=') > -1) {
                        var temp = utils.get_url_param('courtUid');
                        uri = uri.replace('courtUid=' + temp, 'courtUid=' + _param_court_uid);
                    } else {
                        var uri_arr = window.location.href.split('?');
                        uri_arr.length > 1 ? uri += ('&courtUid=' + _param_court_uid) : uri += ('?courtUid=' + _param_court_uid)
                    }
                    window.location.href = decodeURI(uri);
                }
            });
        },
        pagination: function (elm, options) {
            draw(parseInt(options.currentPage), parseInt(options.totalPages));

            function draw(currentPage, totalPages) {
                var page_el = $('<div class="text-center"> <ul class="cmp-pagination"></ul></div>');
                var previous = $('<li><a class="cmp-previous">&#60上一页</a></li>');
                previous.click(function () {
                    //点击上一页
                    var previous_page = currentPage - 1;
                    if (previous_page > 0) {
                        options.currentPage = previous_page;
                        options.onPageClicked(options.currentPage);
                        base.pagination(elm, options);
                    }
                });
                page_el.find('ul').append(previous);
                for (var i = 4; i > 0; i--) {
                    var chemical_i = currentPage - i;
                    if (chemical_i > 0) {
                        var item_i = $('<li><a>' + chemical_i + '</a></li>');
                        item_i.click(function () {
                            //点击当前页之前的
                            options.currentPage = $(this).find('a').html();
                            options.onPageClicked(options.currentPage);
                            base.pagination(elm, options);
                        });
                        page_el.find('ul').append(item_i);
                    }
                }
                var active = $('<li><a class="active">' + currentPage + '</a></li>');//当前页
                page_el.find('ul').append(active);
                for (var j = 1; j < 5; j++) {
                    var chemical_j = currentPage + j;
                    if (chemical_j <= totalPages) {
                        var item_j = $('<li><a>' + chemical_j + '</a></li>');
                        item_j.click(function () {
                            //点击当前页之后的
                            options.currentPage = $(this).find('a').html();
                            options.onPageClicked(options.currentPage);
                            base.pagination(elm, options);
                        });
                        page_el.find('ul').append(item_j);
                    }
                }
                var next = $('<li><a class="cmp-next">下一页&#62</a></li>');
                next.click(function () {
                    //点击下一页
                    var next_page = currentPage + 1;
                    if (next_page <= totalPages) {
                        options.currentPage = next_page;
                        options.onPageClicked(options.currentPage);
                        base.pagination(elm, options);
                    }
                });
                page_el.find('ul').append(next);
                var go = $('<li class="cmp-go-li">跳至<input >页<a class="cmp-go">GO</a></li>');
                go.find('a').click(function () {
                    var go_input = go.find('input');
                    var go_page = go_input.val();
                    go_input.val('');
                    if (go_page > totalPages) {
                        go_page = totalPages;
                    }
                    if (go_page < 1) {
                        go_page = 1;
                    }
                    options.currentPage = go_page;
                    options.onPageClicked(options.currentPage);
                    base.pagination(elm, options);
                });
                page_el.find('ul').append(go);
                var total_ext = $('<li>共<span>' + totalPages + '</span>页</li>');
                page_el.find('ul').append(total_ext);
                $(elm).html('');
                $(elm).append(page_el);
            }
        }
    };
    init();
    window.base = base;
    window.r = ready;
}(window);

;(function ($) {
    $.fn.addBack = $.fn.addBack || $.fn.andSelf;

    $.fn.extend({

        actual: function (method, options) {
            // check if the jQuery method exist
            if (!this[method]) {
                throw '$.actual => The jQuery method "' + method + '" you called does not exist';
            }

            var defaults = {
                absolute: false,
                clone: false,
                includeMargin: false
            };

            var configs = $.extend(defaults, options);

            var $target = this.eq(0);
            var fix, restore;

            if (configs.clone === true) {
                fix = function () {
                    var style = 'position: absolute !important; top: -1000 !important; ';

                    // this is useful with css3pie
                    $target = $target.clone().attr('style', style).appendTo('body');
                };

                restore = function () {
                    // remove DOM element after getting the width
                    $target.remove();
                };
            } else {
                var tmp = [];
                var style = '';
                var $hidden;

                fix = function () {
                    // get all hidden parents
                    $hidden = $target.parents().addBack().filter(':hidden');
                    style += 'visibility: hidden !important; display: block !important; ';

                    if (configs.absolute === true) style += 'position: absolute !important; ';

                    // save the origin style props
                    // set the hidden el css to be got the actual value later
                    $hidden.each(function () {
                        var $this = $(this);

                        // Save original style. If no style was set, attr() returns undefined
                        tmp.push($this.attr('style'));
                        $this.attr('style', style);
                    });
                };

                restore = function () {
                    // restore origin style values
                    $hidden.each(function (i) {
                        var $this = $(this);
                        var _tmp = tmp[i];

                        if (_tmp === undefined) {
                            $this.removeAttr('style');
                        } else {
                            $this.attr('style', _tmp);
                        }
                    });
                };
            }

            fix();
            // get the actual value with user specific methed
            // it can be 'width', 'height', 'outerWidth', 'innerWidth'... etc
            // configs.includeMargin only works for 'outerWidth' and 'outerHeight'
            var actual = /(outer)/.test(method) ?
                $target[method](configs.includeMargin) :
                $target[method]();

            restore();
            // IMPORTANT, this plugin only return the value of the first element
            return actual;
        }
    });
})(jQuery);